<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Account');
Globals::requireTable('Needslog');
Globals::requireTable('Vlevel');

class AccountController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $AccountTable;
	protected $NeedslogTable;
	protected $VlevelTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable      = new UserTable($config);
		$this->ProductTable   = new ProductTable($config);
		$this->OrderTable     = new OrderTable($config);
		$this->AccountTable   = new AccountTable($config);
		$this->NeedslogTable  = new NeedslogTable($config);
		$this->VlevelTable    = new VlevelTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		
	}
	
	/**
	 * 转租列表
	 * uid/token&machine_type
	 */
	public function rentlistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    $list = array();
	    $products = $this->ProductTable->listAll(array('status'=>1,'needs>?'=>3),'rank ASC');
	    foreach ($products as $v){
	        $tmp['img'] = (string)DOMAIN.str_replace('../', '', $v['img']);
	        $tmp['name'] = (string)$v['name'];
	        $tmp['needs'] = (string)$v['needs'];
	        $tmp['pid'] = (string)$v['id'];
	        $list[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$list));
	    exit();
	}
	
	/**
	 * 转租提交
	 * uid/token&machine_type
	 * pid
	 * wechat
	 * username
	 * password
	 * days
	 */
	public function dorentAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $data['pid'] = $this->getIntParam('pid');
	    $data['username'] = $this->getParam('username');
	    $data['password'] = $this->getParam('password');
	    $enddays = $this->getIntParam('days');
	    $wechat = $this->getParam('wechat');
	    if(empty($data['pid']) || empty($data['username']) || empty($data['password']) || (int)$enddays < 1){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
	    
	    $account = $this->AccountTable->getRow(array('username'=>$data['username'],'pid'=>$data['pid'],'endtime>?'=>time(),'status'=>2));
	    if($account['id'] > 0){
	        echo json_encode(array('returncode'=>-3));
	        exit();
	    }
	    
// 	    if(!empty($wechat)){
// 	        $this->UserTable->update(array('wechatID'=>$wechat),$uid);
// 	    }
        $user = $this->UserTable->getRow($uid);
        
        $vlevel = 0;
        $vlevel_list = $this->VlevelTable->listAllWithFields('level',array('uid'=>$uid));
        foreach ($vlevel_list as $k=>$v){
            $vlevel += $v['level'];
        }
	    $status = $vlevel > 500 ? 2 : 1;
	    
	    if($status == 1){
	        $exreas = array(
	            'audit' => '有新账号需要审核！',
	            'pushtype' => 29,
	            'content' => '有新账号需要审核！'
	        );
	        do_jpush(1374,'快去审核吧',$exreas);
	    }
	    
	    //新加入aid，如果有则修改，详情页前端判断过期不显示密码
	    $aid = $this->getIntParam('aid');

	    $date = date('Y-m-d');
	    if($aid < 1){
	        $data['createdate'] = $date;
	        $data['endtime'] = strtotime($date) + 86400 * (int)$enddays;
	        $data['uid'] = $uid;
	        $data['status'] = $status;
	        $data['nums_value'] = 3;
	        $data['nums_show'] = 3;
	        $data['wechat'] = $wechat;
	        $this->AccountTable->add($data);
	    }else{
	        $data_account['createdate'] = $date;
	        $data_account['endtime'] = strtotime($date) + 86400 * (int)$enddays;
	        $data_account['uid'] = $uid;
	        $data_account['status'] = $status;
	        $data_account['nums_value'] = 3;
	        $data_account['nums_show'] = 3;
	        $data_account['wechat'] = $wechat;
	        $data_account['password'] = $data['password'];
	        $data_account['username'] = $data['username'];
	        $data_account['pid'] = $data['pid'];
	        if($status == 2){
	            $data_account['nums_stock'] = 0;
	        }
	        $account = $this->AccountTable->update($data_account,$aid);
	    }
	    
	    $product = $this->ProductTable->getRow($data['pid']);
	    $needs = $product['needs'] - 2;
	    $needs = $needs < 0 ? 0 : $needs;
	    $this->ProductTable->update(array('needs'=>$needs),$data['pid']);
	    
	    
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	/**
	 * 寻租列表
	 * uid/token&machine_type
	 */
	public function needslistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	     
	    $list = array();
	    $products = $this->ProductTable->listAll(array('status'=>1),'rank ASC');
	    foreach ($products as $v){
	        $tmp['img'] = (string)DOMAIN.str_replace('../', '', $v['img']);
	        $tmp['name'] = (string)$v['name'];
	        $tmp['needs'] = (string)$v['needs'];
	        $tmp['pid'] = (string)$v['id'];
	        $list[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$list));
	    exit();
	}
	
	/**
	 * 寻租提交
	 * uid/token&machine_type
	 * pid
	 */
	public function doneedsAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $data['pid'] = $this->getIntParam('pid');
	    if(empty($data['pid'])){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
	    
	    $data['date'] = strtotime(date('Y-m-d'));
	    $data['uid'] = $uid;
	    $isdo = $this->NeedslogTable->getRow($data);
	    if($isdo['id'] > 0){
	        echo json_encode(array('returncode'=>-3));
	        exit();
	    }
	    
	    $this->NeedslogTable->add($data);
	    $product = $this->ProductTable->getRow($data['pid']);
	    $this->ProductTable->update(array('needs'=>$product['needs']+1),$data['pid']);
	    
	    $exreas = array(
	        'income' => '账号有限先到先得哦！',
	        'pushtype' => 10,
	        'content' => '账号有限先到先得哦！'
	    );
	    do_jpush($uid,'小主你想要的账号来了',$exreas);
	    
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	/**
	 * 购买平台列表
	 * uid/token&machine_type
	 */
	public function listAction()
	{
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	     
	    $result = array();
	    $time = time();
	    $products = $this->ProductTable->listAll(array('status'=>1),'rank ASC');
	    foreach ($products as $v){
	        $tmp['pid'] = $v['id'];
	        $tmp['nameline1'] = (string)$v['name'];
	        $tmp['nameline2'] = (string)$v['name_1'];
	        $tmp['nameline3'] = (string)$v['name_2'];
	        $tmp['imageicon'] = (string)DOMAIN.$v['img'];
	        $tmp['imageright'] = (string)DOMAIN.$v['img_1'];
	        $tmp['imagebg'] = (string)DOMAIN.$v['img_2'];
	        $tmp['canbuy'] = '1';
	        $result[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	/**
	 * 购买账号列表
	 * uid/token&machine_type
	 * pid
	 */
	public function daylistAction()
	{
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    $pid = $this->getIntParam('pid');
	    $product = $this->ProductTable->getRow($pid);
	    if(empty($pid) || $product['status'] != 1){
	        echo json_encode(array('returncode'=>-2,'list'=>array()));
	        exit();
	    }
	    
	    $time = time();
	    $time_day = strtotime(date('Y-m-d'));
	    $result = array();
	    $day_arr = array();
	    $accounts = $this->AccountTable->listAll(array('status'=>2,'pid'=>$pid,'nums_value>?'=>0,'endtime>?'=>$time));
	    foreach ($accounts as $v){
	        $day_value = ($v['endtime'] - $time_day) / 86400;
	        $day_value = $day_value == (int)$day_value ? $day_value : (int)$day_value + 1;
	        
	        $ispay = $this->OrderTable->getRowWithFields('id',"uid = {$uid} and aid = {$v['id']} and status > 0");
	        $tmp['ispay'] = $ispay['id'] > 0 ? '1' : '0';
	        
	        if (array_key_exists($day_value,$day_arr)){
	            if($day_arr[$day_value]['ispay'] == 1){
	                continue;
	            }else{
	                if($tmp['ispay'] == 1){
	                    foreach ($result as $k2=>$v2){
	                        if($v2['aid'] == $day_arr[$day_value]['aid']){
	                            unset($result[$k2]);
	                        }
	                    }
	                    unset($day_arr[$day_value]);
	                }
	            }
	        }
	        
	        if (!array_key_exists($day_value,$day_arr)){
	            if($v['nums_stock'] < $v['nums_value']){
        	        $tmp['aid'] = (string)$v['id'];
        	        $tmp['pid'] = (string)$pid;
        	        $tmp['imageicon'] = (string)DOMAIN.$product['img'];
        	        $tmp['imageright'] = (string)DOMAIN.$product['img_1'];
        	        $tmp['imagebg'] = (string)DOMAIN.$product['img_2'];
        	        $tmp['nameline1'] = (string)$product['name'];
        	        $tmp['nameline2'] = (string)$product['name_1'];
        	        $tmp['nameline3'] = (string)$day_value.'天试用期';
        	        $tmp['money'] = round((DAY_MONEY * $day_value + 0.4 - $day_value / 1216),2);
        	        $tmp['days'] = (string)$day_value;
        	        $tmp['canbuy'] = '1';
        	        
        	        $result[] = $tmp;
        	        $day_arr[$day_value] = array('aid'=>$v['id'],'ispay'=>$tmp['ispay']);
	            }
	        }
	    }
	    
	    $result_value = array();
	    foreach ($result as $v){
	        $result_value[] = $v;
	    }
	    
	    echo json_encode(array('returncode'=>0,'list'=>$result_value));
	    exit();
	}
	
	/**
	 * 订单详情
	 * uid/token&machine_type
	 * orderid
	 */
	public function detailAction(){
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'name'=>'','username'=>'','password'=>'','endtime'=>''));
	        exit();
	    }
	    
	    $orderid = $this->getParam('orderid');
	    if(empty($orderid)){
	        echo json_encode(array('returncode'=>-2,'name'=>'','username'=>'','password'=>'','endtime'=>''));
            exit();
	    }
	     
	    $order = $this->OrderTable->getRow(array('orderid'=>$orderid));
	    $product = $this->ProductTable->getRow($order['pid']);
	    $account = $this->AccountTable->getRow($order['aid']);
	    echo json_encode(array('returncode'=>0,'name'=>(string)$product['name'].$order['days'].'天试用期','username'=>(string)$account['username'],'password'=>(string)$account['password'],'endtime'=> (string)date('Y-m-d',$order['endtime'])));
        exit();
	}
	
	/**
	 * 账号详情
	 * uid/token&machine_type
	 * aid
	 */
	public function accountdetailAction(){
	    $returnarr = array(
	        'returncode' => -1,
	        'days' => '',
	        'wechat' => '',
	        'id' => '',
	        'title' => '',
	        'nameline1' => '',
	        'nameline2' => '',
	        'nameline3' => '',
	        'imageicon' => '',
	        'imageright' => '',
	        'imagebg' => '',
	        'money' => '0.00',
	        'money_store' => '0.00',
	        'create_date' => '',
	        'end_date' => '',
	        'nickname' => ''
	    );
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode($returnarr);
	        exit();
	    }
	     
	    $aid = $this->getIntParam('aid');
	    $account = $this->AccountTable->getRow($aid);
	    if(empty($account['id'])){
	        echo json_encode($returnarr);
	        exit();
	    }
	    
	    $product = $this->ProductTable->getRow($account['pid']);
	    $daytime = $account['endtime'] - time();
	    $days = $daytime / 86400;
	    $days = $days == (int)$days ? $days : (int)$days + 1;
	    $days = $days < 0 ? 0 : $days;
	    $user = $this->UserTable->getRow($uid);
	    
	    $returnarr['returncode'] = 0;
	    $returnarr['days'] = (string)$days;
	    $returnarr['wechat'] = (string)$user['wechatID'];
	    $returnarr['id'] = (string)$account['id'];
	    $returnarr['title'] = (string)$product['name'];
	    $returnarr['nameline1'] = (string)$product['name'];
	    $returnarr['nameline2'] = (string)$product['name_1'];
	    $returnarr['nameline3'] = (string)$days.'天试用期';
	    $returnarr['imageicon'] = (string)DOMAIN.$product['img'];
	    $returnarr['imageright'] = (string)DOMAIN.$product['img_1'];
	    $returnarr['imagebg'] = (string)DOMAIN.$product['img_2'];
	    $returnarr['money'] = round((DAY_MONEY * $days + 0.4 - $days / 1216),2);
	    $returnarr['money_store'] = $returnarr['money'] * 2;
	    $returnarr['create_date'] = (string)date('Y-m-d');
	    $returnarr['end_date'] = (string)date('Y-m-d',$account['endtime']);
	    $returnarr['nickname'] = (string)$user['nickname'];
	    $returnarr['canbuy'] = '1';
	    echo json_encode($returnarr);
	    exit();
	}
	
	/**
	 * 账号详情（购买后）
	 * uid/token&machine_type
	 * aid
	 */
	public function rentaccountdetailAction(){
	    $returnarr = array(
	        'returncode' => -1,
	        'days' => '',
	        'wechat' => '',
	        'id' => '',
	        'title' => '',
	        'nameline1' => '',
	        'nameline2' => '',
	        'nameline3' => '',
	        'imageicon' => '',
	        'imageright' => '',
	        'imagebg' => '',
	        'money' => '0.00',
	        'money_store' => '0.00',
	        'create_date' => '',
	        'end_date' => '',
	        'username' => '',
	        'password' => '',
	        'nickname' => '',
	        'pid' => ''
	    );
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode($returnarr);
	        exit();
	    }
        
	    $aid = $this->getIntParam('aid');
	    $account = $this->AccountTable->getRow($aid);
	    if(empty($account['id'])){
	        echo json_encode($returnarr);
	        exit();
	    }
	    
	    $product = $this->ProductTable->getRow($account['pid']);
	    $daytime = $account['endtime'] - time();
	    if($daytime > 0){
	        $days = $daytime / 86400;
	        $days = $days == (int)$days ? $days : (int)$days + 1;
	        $days = $days < 0 ? 0 : $days;
	    }else{
	        $days = 0;
	    }
	    
	    $user = $this->UserTable->getRow($uid);
	    
	    $returnarr['returncode'] = 0;
	    $returnarr['days'] = (string)$days;
	    $returnarr['wechat'] = (string)$user['wechatID'];
	    $returnarr['id'] = (string)$account['id'];
	    $returnarr['title'] = (string)$product['name'];
	    $returnarr['nameline1'] = (string)$product['name'];
	    $returnarr['nameline2'] = (string)$product['name_1'];
	    $returnarr['nameline3'] = (string)$days.'天试用期';
	    $returnarr['imageicon'] = (string)DOMAIN.$product['img'];
	    $returnarr['imageright'] = (string)DOMAIN.$product['img_1'];
	    $returnarr['imagebg'] = (string)DOMAIN.$product['img_2'];
	    $returnarr['money'] = (string)$account['money'];
	    $returnarr['money_store'] = (string)$account['money'] * 2;
	    $returnarr['create_date'] = (string)date('Y-m-d');
	    $returnarr['end_date'] = (string)date('Y-m-d',$account['endtime']);
	    $returnarr['nickname'] = (string)$user['nickname'];
	    $returnarr['username'] = (string)$account['username'];
	    $returnarr['password'] = (string)$account['password'];
	    //$returnarr['username'] = '******';//2017-05-02 李东东
	    //$returnarr['password'] = '******';//2017-05-02 李东东
	    $returnarr['wechat'] = (string)$account['wechat'];
	    $returnarr['pid'] = (string)$product['id'];
	    echo json_encode($returnarr);
	    exit();
	}
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('AccountController', 'Controller');
